package com.new_blog.controller;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-03-23
 * Time:15:23
 * Text：
 */

import com.new_blog.common.AppVarible;
import com.new_blog.common.ResultAjax;
import com.new_blog.common.SessionUtils;
import com.new_blog.model.Userinfo;
import com.new_blog.service.UserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@RestController
@RequestMapping("/api")
public class FileUploadController {
    private String separator = File.separator;//获取操作系统的文件分隔符
    @Autowired
    private ResourceLoader resourceLoader;
    @Autowired
    private UserinfoService userService;
    @PostMapping("/upload")
    public ResultAjax upload(@RequestParam("file") MultipartFile file,HttpServletRequest request) throws IOException {
        // 判断是否为空文件
        if (file.isEmpty()) {
            return ResultAjax.fail(-1,"上传失败");
        }
        Userinfo user= SessionUtils.getUser(request);
        if(user==null){
            return ResultAjax.fail(-1,"非法请求");
        }

        /*
             // 获取文件名
                String fileName = file.getOriginalFilename();
             // 获取文件后缀
                String suffixName = fileName.substring(fileName.lastIndexOf("."));
            // 重新生成文件名
                fileName = UUID.randomUUID() + suffixName;*/

        // 获取文件名
        String fileName = user.getUid()+".jpg";
        // 设置文件存储路径
        Path currentDir = Paths.get("images");
        Path filePath = currentDir.toAbsolutePath();
        if (!Files.exists(filePath)) {
            Files.createDirectory(filePath);
        }
        File dest = new File(filePath + separator  + fileName);
        try {
            if (dest.exists()) {
                // 如果存在同名文件，先删除
                dest.delete();
            }else{
                //更换数据库的照片路径
                user.setPhoto(fileName);
                HttpSession session = request.getSession();
                session.setAttribute(AppVarible.SESSION_USERINFO_KEY, user);
                userService.saveOrUpdate(user);
            }
            // 保存文件
            file.transferTo(dest);
            return ResultAjax.succ(200,"上传成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResultAjax.fail(-1,"上传失败");
    }
    @PostMapping ("/log")
    public ResultAjax log(HttpServletRequest request){
        Userinfo user= SessionUtils.getUser(request);
        if(user==null || user.getPhoto().equals("./img/logo2.jpg")){
            return ResultAjax.fail(-1,"请求失败");
        }
        // 获取文件名
        String fileName ="images\\"+ user.getPhoto();
        String imageUrl= fileName;
        return ResultAjax.succ(imageUrl);
    }
}

